public class Prefix {
    //将nums划分成三段，nums[i]，nums[i]左边，nums[i]右边
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int[] leftDp = new int[n];//左前缀积
        int[] rightDp = new int[n];//右前缀积
        int[] answer = new int[n];

        leftDp[0] = 1;
        rightDp[n - 1] = 1;
        for (int i = 1; i < n; i++) {
            leftDp[i] = leftDp[i - 1] * nums[i - 1];
            rightDp[n - i - 1] = rightDp[n - i] * nums[n - i];
        }
        for (int i = 0; i < n; i++) {
            answer[i] = leftDp[i] * rightDp[i];
        }
        return answer;
    }
}
